<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/12
 * Time: 10:46
 */

namespace app\home\controller;
use think\Request;
// 允许所有跨域请求
header('Access-Control-Allow-Origin: *');
class Link extends common
{
    /**
     * 生成推广链接
     */
    public function makeLink(){
        $user = session('user');
        $uid = $user['id'];
        if(Request::instance()->isPost()){
            $rebate = input('post.rebate/d');
            if($rebate < 0){
                return json_encode(['msg'=>0,'info'=>'参数非法']);
            }
            $data['uid'] = $uid;
            $data['rebate'] = $rebate;
            $data['lv'] = 1;
            $data['time'] = date('Y-m-d h:i:s');
            $data['p'] =substr(md5($uid.time()),8,16);
            //判断网站设置的上下级最小返点差
            $cha = db('webconfig')->where('id',1)->value('return_min');
            if($rebate+$cha > $user['rebate']){
                return json_encode(['msg'=>0,'info'=>'生成失败,允许上下级最小差为'.$cha.'%']);
            }
            if(db('link')->insert($data)){
                return json_encode(['msg'=>1,'info'=>'生成成功!','link'=>url('Link/askLink', '',  true,  true).'?p='.$data['p']]);
            }else{
                return json_encode(['msg'=>0,'info'=>'生成失败!']);
            }

        }else{
            $this->assign('rebate',$user['rebate']);
            return $this->fetch('common/setshare');
        }

    }

    /**
     * 请求生成的推广链接
     */
    public function askLink(){
        if(Request::instance()->isPost()){
            $p = input('get.p');
            $gx = db('link')->where('p',$p)->value('uid');
            if($gx){
                if(input('?post.name') && input('?post.pass') && input('?post.xpass') && input('?post.qq') && input('?post.rebate')){
                    if(input('?post.pass') != input('?post.xpass')){
                        $this->error('两次密码不同');
                    }
                    if(db('user')->where('name',input('post.name'))->find()){
                        $this->error('用户名已被占用!');
                    }
                    $data['gx'] = $gx;
                    $data['name'] = input('post.name');
                    $data['pass'] = md5(input('post.pass'));
                    $data['rebate'] = input('post.rebate');
                    $data['qq'] = input('post.qq');
                    $data['user_pic'] = 'pic.jpg';
                    $data['login_time'] = time();
                    $data['login_ip'] =  request()->ip();
                    if(db('user')->insert($data)){
                        $this->success('注册成功!','login/login');
                    }else{
                        $this->error('注册失败');
                    }

                }else{
                    $this->error('参数非法');
                }

            }else{
                return json_encode(['msg'=>2,'info'=>'无效链接!']);
            }
        }else{
            $p = input('get.p');
            $rebate = db('link')->where('p',$p)->value('rebate');
            if(!$rebate){
                $this->error('无效链接');
            }
            $this->assign('rebate',$rebate);
            return $this->fetch('common/linkadduser');
        }

    }
}